/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.netbrain.bean;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import mx.netbrain.dao.TypeFileDao;
import mx.netbrain.dao.impl.TypeFileDaoImpl;
import mx.netbrain.model.Type;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author Julian
 */
@ManagedBean(name = "fileUploadController")
@SessionScoped
public class TypeFileBean {

    private String destination = "C:\\netbrain\\";
    private UploadedFile file;
    private Type typeFile = new Type();
    private List<Type> listTypeFile;

    /**
     * Creates a new instance of TypeFileBean
     */
    public TypeFileBean() {
    }

    public void findById(Long id) {
        TypeFileDao typeFileDao = new TypeFileDaoImpl();
        typeFile = typeFileDao.findById(id);
    }
    
    public void handleFileUpload(FileUploadEvent event) {
        FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
        file = event.getFile();
        uploadValidate();
    }

    public void transferFile(String fileName, InputStream in) {
        try {
            OutputStream out = new FileOutputStream(new File(destination + fileName));
            int reader = 0;
            byte[] bytes = new byte[(int) getFile().getSize()];
            while ((reader = in.read(bytes)) != -1) {
                out.write(bytes, 0, reader);
            }
            in.close();
            out.flush();
            out.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    public void uploadValidate() {
        String extValidate;
        if (getFile() != null) {
            String ext = getFile().getFileName();
            if (ext != null) {
                extValidate = ext.substring(ext.indexOf(".") + 1);
            } else {
                extValidate = null;
            }
            if (extValidate.equals("png")) {
                try {
                    transferFile(getFile().getFileName(), getFile().getInputstream());
                } catch (IOException e) {
                    FacesContext context = FacesContext.getCurrentInstance();
                    context.addMessage(null, new FacesMessage("Wrong", "Error al cargar archivo."));
                }
                typeFile.setExtension(extValidate);
                typeFile.setName(getFile().getFileName());
                typeFile.setRute(destination);
                typeFile.setType(getFile().getContentType());
                TypeFileDao typeFileDao = new TypeFileDaoImpl();
                typeFileDao.save(typeFile);
                typeFile = new Type();
                FacesContext context = FacesContext.getCurrentInstance();
                context.addMessage(null, new FacesMessage("Succefull", getFile().getFileName() + " se cargo correctamente!"));
            } else {
                FacesContext context = FacesContext.getCurrentInstance();
                context.addMessage(null, new FacesMessage("Wrong", "Solo extensiones PNG."));
            }
        } else {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage("Wrong", "Seleccione un archivo."));
        }
    }

    public void delete() {
        TypeFileDao typeFileDao = new TypeFileDaoImpl();
        typeFileDao.delete(typeFile);
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Se ha borrado el tipo de archivo correctamente!", "Actualizado");
        FacesContext.getCurrentInstance().addMessage(null, message);
        typeFile = new Type();
    }

    public String getDestination() {
        return destination;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }

    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;
    }

    public Type getTypeFile() {
        return typeFile;
    }

    public void setTypeFile(Type typeFile) {
        this.typeFile = typeFile;
    }

    public List<Type> getListTypeFile() {
        TypeFileDao typeFileDao = new TypeFileDaoImpl();
        listTypeFile = typeFileDao.listAll();
        return listTypeFile;
    }

    public void setListTypeFile(List<Type> listTypeFile) {
        this.listTypeFile = listTypeFile;
    }
}
